package com.nan.hrm.service.impl;

import com.nan.hrm.LoginUserClient;
import com.nan.hrm.domain.LoginUser;
import com.nan.hrm.domain.VipBase;
import com.nan.hrm.domain.VipUser;
import com.nan.hrm.dto.RegisterDto;
import com.nan.hrm.dto.SmsCodeDto;
import com.nan.hrm.mapper.VipBaseMapper;
import com.nan.hrm.mapper.VipUserMapper;
import com.nan.hrm.service.IVipUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.nan.hrm.util.AjaxResult;
import com.nan.hrm.util.ValidUtils;
import com.nan.hrm.util.encrypt.MD5;
import com.nan.hrm.util.exception.GlobalException;
import com.nan.hrm.vo.SMSCodeRedisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author N
 * @since 2021-09-21
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {
    @Autowired
    private VipUserMapper vipUserMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private VipBaseMapper vipBaseMapper;
    @Autowired
    private LoginUserClient loginUserClient;
    @Override
    public void register(RegisterDto registerDto) {
        // 参数判断
        String mobile = registerDto.getMobile();
        String password = registerDto.getPassword();
        String imageCode = registerDto.getImageCode();
        String smsCode = registerDto.getSmsCode();
        ValidUtils.assertNotNull(mobile,"电话不能为空");
        // 判断手机号是否被注册
        VipUser vipUser = vipUserMapper.selectByPhone(mobile);
        if(vipUser!=null){
            throw new GlobalException("手机号已经被注册");
        }
        // 判断手机验证码是否正确
        // 在redis中取验证码（先封装key）
        String rediskey = SMSCodeRedisVo.SMS_CODE_KEY_REG + mobile;
        SMSCodeRedisVo smsInredis = (SMSCodeRedisVo) redisTemplate.opsForValue().get(rediskey);
        if(smsInredis==null || smsCode.equals(smsInredis)){
            throw new GlobalException("验证码错误或过期");
        }

        // 先保存login_user表
        LoginUser loginUser = new LoginUser(mobile,password,0);
        AjaxResult ajaxResult = loginUserClient.addOrUpdate(loginUser);

        //再保存vipuser表
        VipUser vipUser1 = new VipUser();
        //创建时间
        vipUser1.setCreateTime(System.currentTimeMillis());
        vipUser1.setPhone(mobile);
        vipUser1.setBitState(VipUser.BIT_STATE_PHONE);
//        System.out.println(loginUser.getId());
        vipUser1.setLoginId((Long) ajaxResult.getResultObj());

        //盐值加密
        String salt = UUID.randomUUID().toString().substring(0,8);
        vipUser1.setSalt(salt);

        String md5 = MD5.getMD5(mobile + salt);
        vipUser1.setPassword(md5);
        vipUserMapper.insert(vipUser1);

        //保存vipbase表
        VipBase vipBase = new VipBase();
        vipBase.setCreateTime(System.currentTimeMillis());
        vipBase.setSsoId(vipUser1.getId());
        vipBase.setRegTime(System.currentTimeMillis());
        vipBaseMapper.insert(vipBase);
    }
}
